package com.skyc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.skyc.entity.Clothes;
import com.skyc.entity.Order;
import com.skyc.entity.Shop;
import com.skyc.entity.User;
import com.skyc.service.ClothesService;
import com.skyc.service.OrderService;
import com.skyc.service.impl.ClothesServiceImpl;
import com.skyc.service.impl.OrderServiceImpl;

/**
 * Servlet implementation class AServlet
 */
@WebServlet("/ToOrderPayServlet")
public class ToOrderPayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	HttpSession session = null;
	PrintWriter out = null;
	User user = null;
	Gson gson=null;
	OrderService os=null;
	List<Order> orderlist=null;//订单列表
	List<String> imgurllist=null;//图片路径列表
	String time=null;//购买时间
	int sumprice=0;//总价
	String ordername="/";   //订单名称
	ClothesService cs=null;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		doPost(req, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		session = request.getSession();
		ServletContext application = getServletContext();
		out = response.getWriter();
		os=new OrderServiceImpl();
		cs=new ClothesServiceImpl();
		Date date=new Date();//获取当前时间
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化时间
		 time=sdf.format(date);	
		long ordernum= date.getTime();
		session.setAttribute("ordernum", ordernum);//订单号	
		System.out.println(ordernum);
		
		user = (User) session.getAttribute("user");
		if (user != null) {
			String flag = request.getParameter("flag");
			if (flag != null) {
				switch (flag) {
				case "shopcartflag":
					scitem(request, response);
					break;
				case "itemshowflag":
					isitem(request, response);
					break;

				default:
					break;
				}
			}

		} else {
			out.print("<script>alert('请先登录账号');location='login.jsp';</script>");
		}

		out.flush();
		out.close();
		
	}

	private void isitem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cid=request.getParameter("cid");
		int sum=Integer.valueOf(request.getParameter("sum"));
		System.out.println(cid);
		System.out.println(sum);
		
		orderlist=new ArrayList<Order>();
		imgurllist=new ArrayList<String>();	
		if(!cid.equals("0")) {
			Clothes clothes=cs.queryByID(cid);
			if(clothes!=null) {
				String imgurl=clothes.getCimgurl().split("/")[0];	
				imgurllist.add(imgurl);
				
				Order order =new Order();
				order.setUid(user.getUid());
				order.setClothes(clothes);
				order.setOnum(sum);
				order.setOprice(sum*clothes.getCprice());
				order.setOorder(0);
				order.setOpayed(0);
				order.setOsend(0);
				order.setOsuccess(0);
				order.setOtuihuo(0);
				order.setOstate(0);
				
				sumprice=sum*clothes.getCprice();
				ordername=clothes.getCname();
				boolean flagos= os.addOrder(order);
				if(flagos) {
					orderlist.add(order);
				}else {
					System.out.println("添加订单失败");
				}	
				
			}else {
				System.out.println("返回clothes失败");
			}
			System.out.println(ordername);
			List<Integer> oid=os.queryfirstoid(1);
			session.setAttribute("oid", oid);
			session.setAttribute("flag", 2);
			request.setAttribute("ordername", ordername);//订单名称
			request.setAttribute("time", time);//当前时间
			request.setAttribute("sumprice", sumprice);//总价
			request.setAttribute("imgurllist", imgurllist);//图片路径列表
			request.setAttribute("orderlist", orderlist);//订单列表
			request.getRequestDispatcher("skyc_orderpay.jsp").forward(request, response);
		}else {
			out.print("<script>alert('这件衣服没有库存了！');history.go(-1);</script>");
		}		
//	String strorder=request.getParameter("order");	
//	gson =new Gson();
//	Order order=gson.fromJson(strorder,Order.class);//字符串转化为对象
//	System.out.println(order);
//	String imgurl=order.getClothes().getCimgurl().split("/")[0];
//	request.setAttribute("order", order);
//	request.setAttribute("imgurl", imgurl);
//	request.getRequestDispatcher("skyc_orderpay.jsp").forward(request, response);
			
	}

	private void scitem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		List<Shop> shoplist=(List<Shop>) session.getAttribute("pay_shops");	
		orderlist=new ArrayList<Order>();
		imgurllist=new ArrayList<String>();		
		for (Shop shop : shoplist) {
		String imgurl=shop.getClothes().getCimgurl().split("/")[0];	
		imgurllist.add(imgurl);
		
		Order order =new Order();
		order.setUid(user.getUid());
		order.setClothes(shop.getClothes());
		order.setOnum(shop.getSnum());
		order.setOprice(shop.getSnum()*shop.getClothes().getCprice());
		order.setOorder(0);
		order.setOpayed(0);
		order.setOsend(0);
		order.setOsuccess(0);
		order.setOtuihuo(0);
		order.setOstate(0);
		
		sumprice+=shop.getSnum()*shop.getClothes().getCprice();
		ordername+=shop.getClothes().getCname();
		boolean flagos= os.addOrder(order);
		if(flagos) {
			orderlist.add(order);
		}else {
			System.out.println("添加订单失败");
		}	
        }
		System.out.println(ordername);
		List<Integer> oid=os.queryfirstoid(shoplist.size());
		session.setAttribute("oid", oid);
		session.setAttribute("flag", 2);
		request.setAttribute("ordername", ordername);//订单名称
		request.setAttribute("time", time);//当前时间
		request.setAttribute("sumprice", sumprice);//总价
		request.setAttribute("imgurllist", imgurllist);//图片路径列表
		request.setAttribute("orderlist", orderlist);//订单列表
		request.getRequestDispatcher("skyc_orderpay.jsp").forward(request, response);
		
		
	}

}
